package com.example.zzl.view.activity.SearchActivity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.adapter.SearchFragment.SCAdapter;
import com.example.zzl.app.MyApplication;
import com.example.zzl.databinding.ActivitySearchBinding;
import com.example.zzl.model.cans.Cans;
import com.example.zzl.utils.StatusBarUtils;
import com.example.zzl.viewmodel.SearchFragment.Classify;
import com.example.zzl.viewmodel.SearchFragment.ResultData;

import java.util.ArrayList;

public class SearchActivity extends AppCompatActivity implements TextView.OnEditorActionListener{

    private ActivitySearchBinding binding;
    private EditText et ;
    private Classify classify;
    public static ResultData rd;
    public static Handler handler;
    private ListView lv;
    private ArrayList<String> data;
    private SCAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        rd = new ResultData();
        StatusBarUtils.setWindowStatusBarColor(SearchActivity.this, android.R.color.holo_red_light);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_search);
        init();

    }

    private void init() {
        et = binding.etSearch1;
        lv = binding.lvSearch;
        data = new ArrayList<>();
        adapter = new SCAdapter(this,data);
        lv.setAdapter(adapter);
        et.setOnEditorActionListener(this);
        editaCtualTime();



    }

    private void editaCtualTime() {
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String data1 = et.getText().toString();
                data.clear();
                adapter.notifyDataSetChanged();
                if(!TextUtils.isEmpty(data1)){
                    classify = new Classify(Cans.SEARCH_MATE+data1,"Mate");
                    loadData();
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {
                //Toast.makeText(SearchActivity.this, "4444", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void loadData(){
        data.clear();
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 0x401){
                    //Log.e("data",rd.mates.size()+"");
                    if(rd.mates.size()>=2){
                        data.add(rd.mates.get(0).getStrGroupName());
                        for (int i = 0; i < rd.mates.get(0).getListHotTag().size(); i++) {
                            data.add(rd.mates.get(0).getListHotTag().get(i).getStrTagEngName()+" "+rd.mates.get(0).getListHotTag().get(i).getStrTagName() );
                        }
                        data.add(rd.mates.get(1).getStrGroupName());
                        for (int i = 0; i < rd.mates.get(1).getListHotTag().size(); i++) {
                            data.add(rd.mates.get(1).getListHotTag().get(i).getStrTagName());
                            //Log.e("data",rd.mates.get(1).getListHotTag().get(i).getStrTagName());
                        }
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        };
    }

    public void click(View view){
        switch (view.getId()){
            case R.id.tv_back:
                setResult(101,getIntent());
                finish();
                break;
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId,KeyEvent event) {
        if (actionId== EditorInfo.IME_ACTION_SEND ||
                (event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) {
            if(!et.getText().toString().equals("")) {
                Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
                intent.putExtra("search",et.getText().toString());
                intent.putExtra("type","");
                startActivityForResult(intent, 444);
                et.setImeOptions(EditorInfo.IME_ACTION_DONE);
            }else{
                Animation shake = AnimationUtils.loadAnimation(SearchActivity.this, R.anim.et_error);
                et.startAnimation(shake);
            }
            return true;
        }
        return false;
    }
}
